﻿using DHQGHN.DAL.BO;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.DHQG;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.BO.DHQG
{
    public class NguoiDungBO: BaseBO
    {
        private NguoiDungDAO _sqlBO;
        public const string DB_COLUMNS_NGUOI_DUNG_ID = "NGUOI_DUNG_ID";
        public const string DB_COLUMNS_NHOM_DON_VI_ID = "NHOM_DON_VI_ID";
        public const string DB_COLUMNS_DON_VI_ID = "DON_VI_ID";
        public const string DB_COLUMNS_TEN_DON_VI = "TEN_DON_VI";
        public const string DB_COLUMNS_HO_VA_TEN = "HO_VA_TEN";
        public const string DB_COLUMNS_TAI_KHOAN = "TAI_KHOAN";
        public const string DB_COLUMNS_DIEN_THOAI = "DIEN_THOAI";
        public const string DB_COLUMNS_DELETED = "DELETED";
        public const string DB_COLUMNS_IS_ADMIN = "IS_ADMIN";
        public const string DB_COLUMNS_IS_FIX = "IS_FIX";
        public const string DB_COLUMNS_LAST_UPDATED = "LAST_UPDATED";

        public NguoiDungBO()
        {
            _sqlBO = new NguoiDungDAO(Common.ConnectionUtil.DHQGHN_CONNECTION_STRING, DEFAULT_MAPPER_TYPE);
        }

        public NguoiDungBO(string connectionString)
        {
            _sqlBO = new NguoiDungDAO(connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<NguoiDung> GetListPaging(NguoiDung model, int pageIndex, int pageSize)
        {
            try
            {
                Dictionary<string, string> mapDictionary = GetMapDictionary();
                return _sqlBO.GetListPaging(model, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public NguoiDung GetById(int nguoiDungId)
        {
            try
            {
                Dictionary<string, string> mapDictionary = GetMapDictionary();
                return _sqlBO.GetById(nguoiDungId, mapDictionary);
            }
            catch
            {

                throw;
            }
        }

        public bool Save(NguoiDung model)
        {
            try
            {
                return _sqlBO.Save(model);   
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int[] nguoiDungId)
        {
            try
            {
                return _sqlBO.Delete(nguoiDungId);
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int nguoiDungId)
        {
            try
            {
                return _sqlBO.Delete(nguoiDungId);
            }
            catch
            {
                throw;
            }
        }

        public NguoiDung GetUserInforFromPM2(string UserName)
        {
            try
            {
                Dictionary<string, string> mapDictionary = GetMapDictionary();
                return _sqlBO.GetUserInforFromPM2(UserName, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        

        //public List<ChiTieu> GetListChiTieu(string UserName)
        //{
        //    try
        //    {
        //        Dictionary<string, string> mapDictionary = GetMapDictionary();
        //        return _sqlBO.GetListChiTieu(UserName, mapDictionary);
        //    }
        //    catch
        //    {
        //        throw;
        //    }
        //}

        public bool CheckUserNameExist(string userName)
        {
            try
            {
                NguoiDung user = _sqlBO.GetUserInforFromPM2(userName, GetMapDictionary());
                if (user == null)
                {
                    return false;
                }

                return true;
            }
            catch
            {
                throw;
            }
        }

        public List<NguoiDung> GetListUserByDonViId(int donViId)
        {
            try
            {
                Dictionary<string, string> mapDictionary = GetMapDictionary();
                return _sqlBO.GetListUserByDonViId(donViId, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public bool UpdateQuyenChot(int nguoiDungId, int isFix)
        {
            try
            {
                return _sqlBO.UpdateQuyenChot(nguoiDungId, isFix);
            }
            catch
            {
                throw;
            }
        }

        public bool Reactive(int nguoiDungId)
        {
            try
            {
                return _sqlBO.Reactive(nguoiDungId);
            }
            catch
            {

                throw;
            }
        }

        private Dictionary<string, string> GetMapDictionary()
        {
            Dictionary<string, string> mapDictionary = new Dictionary<string, string>()
            {
               {NguoiDung.FIELD_NGUOI_DUNG_ID, NguoiDungBO.DB_COLUMNS_NGUOI_DUNG_ID},
               {NguoiDung.FIELD_NHOM_DON_VI_ID, NguoiDungBO.DB_COLUMNS_NHOM_DON_VI_ID},
               {NguoiDung.FIELD_DON_VI_ID, NguoiDungBO.DB_COLUMNS_DON_VI_ID},
               {NguoiDung.FIELD_HO_VA_TEN, NguoiDungBO.DB_COLUMNS_HO_VA_TEN},
               {NguoiDung.FIELD_TAI_KHOAN, NguoiDungBO.DB_COLUMNS_TAI_KHOAN},
               {NguoiDung.FIELD_DIEN_THOAI, NguoiDungBO.DB_COLUMNS_DIEN_THOAI},
               {NguoiDung.FIELD_IS_ADMIN, NguoiDungBO.DB_COLUMNS_IS_ADMIN},
               {NguoiDung.FIELD_IS_FIX, NguoiDungBO.DB_COLUMNS_IS_FIX},
               {NguoiDung.FIELD_DELETED, NguoiDungBO.DB_COLUMNS_DELETED},
               {NguoiDung.FIELD_LAST_UPDATED, NguoiDungBO.DB_COLUMNS_LAST_UPDATED},
            };

            return mapDictionary;
        }
    }
}
 